package com.earnings.okhttputils.utils.ui.activity.common;

import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.earnings.R;
import com.earnings.okhttputils.utils.OkHttp.OkHttpUtils;
import com.earnings.okhttputils.utils.OkHttp.utils.ToastUtils;
import com.earnings.okhttputils.utils.UrlConstant.Constant;
import com.earnings.okhttputils.utils.UrlConstant.HttpUrl;
import com.earnings.okhttputils.utils.bean.RecordOrderData;
import com.earnings.okhttputils.utils.bean.RecordPlanData;
import com.earnings.okhttputils.utils.god.GodAdapter.GodBaseAdapter;
import com.earnings.okhttputils.utils.god.GodAdapter.GodViewHolder;
import com.earnings.okhttputils.utils.god.GodLeftHandBaseActivity;
import com.earnings.okhttputils.utils.god.Listener.GodOnClickListener;
import com.earnings.okhttputils.utils.https.HttpMap;
import com.earnings.okhttputils.utils.https.HttpObjectCallback;
import com.earnings.okhttputils.utils.ui.activity.user.UserHelpActivity;
import com.earnings.okhttputils.utils.utils.TimestampTransform;
import com.earnings.okhttputils.utils.view.FullStaggeredGridLayoutManager;

import java.util.ArrayList;
import java.util.Map;

import static com.earnings.okhttputils.utils.UrlConstant.Constant.RECORD_TYPE_PAY_OFFLINE;


/*
收款页面-收款记录详情
 */
public class RecordDetailsActivity extends GodLeftHandBaseActivity implements GodOnClickListener {


    private int orderTypeid = Constant.RECORD_TYPE_ALL;

    @Override
    protected int onCreateLayoutId() {
        if (bundleData.containsKey( "type" ))
            orderTypeid = bundleData.getInt( "type" );
        if (orderTypeid == Constant.RECORD_TYPE_MONEY_TURN || orderTypeid == Constant.RECORD_TYPE_MONEY_RETURN) {

            return R.layout.activity_record_turn_details;
        }  if (orderTypeid == Constant.RECORD_TYPE_UP_LEAVEL||orderTypeid == Constant.RECORD_TYPE_RECHARGE_ONLINE|| orderTypeid == RECORD_TYPE_PAY_OFFLINE) {

            return R.layout.activity_record_biz_up;
        } else if (orderTypeid == Constant.RECORD_TYPE_RECHARGE_PHONE || orderTypeid == Constant.RECORD_TYPE_RECHARGE_FLOW || orderTypeid == Constant.RECORD_TYPE_RECHARGE_OIL) {

            return R.layout.activity_record_recharge_details;
        } else if (orderTypeid == Constant.RECORD_TYPE_WITHDRAWALS){
            return R.layout.activity_record_money;
        }else if (orderTypeid == Constant.RECORD_TYPE_RED){
            return R.layout.activity_record_red_details;
        }


        return R.layout.activity_record_turn_details;
    }

    @Override
    protected void init() {

        setTitle( "账单详情" );
        setOnClickListener( this, R.id.sumbit );
        loadData();
    }

    public void initData(RecordOrderData data) {

        if (orderTypeid == Constant.RECORD_TYPE_MONEY_TURN || orderTypeid == Constant.RECORD_TYPE_MONEY_RETURN ) {
            setGlideImagView( R.id.head, data.getHead_pic() );
            setText( R.id.name, data.getReciprocal() );
            setText( R.id.price, data.getMoney() );
            setText( R.id.status, data.getOrder_status_note() );
            setText( R.id.paytype, getPayType( data.getPay_code() ) );
            setText( R.id.time, data.getPay_time() );
            setText( R.id.number, data.getOrder_sn() );
            setText( R.id.trade, data.getTrade_type() );
            setText( R.id.account, data.getReciprocal() );

        } else if (orderTypeid == Constant.RECORD_TYPE_RECHARGE_PHONE || orderTypeid == Constant.RECORD_TYPE_RECHARGE_FLOW || orderTypeid == Constant.RECORD_TYPE_RECHARGE_OIL ) {
            setGlideImagView( R.id.head, data.getHead_pic() );
            setText( R.id.name, data.getFacilitator() );
            setText( R.id.price, data.getTotalmoney() );
            setText( R.id.money, data.getMoney() );
            setText( R.id.phone, data.getNumber() );
            setText( R.id.status, data.getOrder_status_note() );
            setText( R.id.paytype, getPayType( data.getPay_code() ) );
            setText( R.id.time, data.getPay_time() );
            setText( R.id.number, data.getOrder_sn() );
            setText( R.id.trade, data.getTrade_type() );
            setText( R.id.account, data.getReciprocal() );
            setText( R.id.sutime, data.getCishu() );
            setText( R.id.sudate, data.getNext_execution_date() );
            setText( R.id.biznumber, data.getTrade_no() );
            if(data.getPlan().size()!=0)
                initSubisidy( (ArrayList<RecordPlanData>) data.getPlan() );
        }
        else if (orderTypeid == Constant.RECORD_TYPE_UP_LEAVEL){
            setGlideImagView( R.id.head, data.getHead_pic() );
            setText( R.id.name, data.getReciprocal() );
            setText( R.id.price, data.getMoney() );
            setText( R.id.status, data.getOrder_status_note() );
            setText( R.id.paytype, getPayType( data.getPay_code() ) );
            setText( R.id.time, data.getPay_time() );
            setText( R.id.number, data.getOrder_sn() );
            setText( R.id.trade, data.getTrade_type() );
            setText( R.id.bizinfo, data.getGood_info() );
        } else if (orderTypeid == Constant.RECORD_TYPE_PAY_OFFLINE){
            setGlideImagView( R.id.head, data.getStore_logo() );
            setText( R.id.name, data.getReciprocal() );
            setText( R.id.price, data.getMoney() );
            setText( R.id.status, data.getOrder_status_note() );
            setText( R.id.paytype, getPayType( data.getPay_code() ) );
            setText( R.id.time, data.getAdd_time() );
            setText( R.id.number, data.getOrder_sn() );
            setText( R.id.trade, data.getTrade_type() );
           setText( R.id.bizinfo, data.getGoods_name() );
        }
        else if (orderTypeid == Constant.RECORD_TYPE_RECHARGE_ONLINE){
            setGlideImagView( R.id.head, data.getHead_pic() );
            setText( R.id.name, data.getReciprocal() );
            setText( R.id.price, data.getAccount() );
            setText( R.id.status, data.getOrder_status_note() );
            setText( R.id.paytype, getPayType( data.getPay_code() ) );
            setText( R.id.time, data.getPay_time() );
            setText( R.id.number, data.getOrder_sn() );
            setText( R.id.trade, data.getPay_name() );
            setText( R.id.bizinfo, "在线充值" );
            setVisibility( R.id.biz,View.VISIBLE );
            setText( R.id.biznumber, data.getTrade_no() );

        }
        else if (orderTypeid ==Constant.RECORD_TYPE_WITHDRAWALS){
            setText( R.id.price, data.getMoney() );
            setText( R.id.name, data.getBank_name() );
            setText( R.id.info, data.getMode() );
            setText( R.id.status, data.getOrder_status_note() );
            if(data.getStatus().equals( "1" )||data.getStatus().equals( "2" )){
                getView( R.id.line ).setBackgroundColor( getResources().getColor( R.color.style ) );
                setImageResource( R.id.right,R.mipmap.check_true );
                setTextColor( R.id.statusinfo,  getResources().getColor( R.color.style ) );
            }
            setText( R.id.statusinfo, data.getOrder_status_note());
            setText( R.id.trade, data.getReciprocal());

            setText( R.id.time,TimestampTransform.TimeStamp2Date(  data.getAdd_time()) );
            setText( R.id.number, data.getOrder_sn() );
           // setText( R.id.biznumber, data.get() );
        }else if ( orderTypeid ==Constant.RECORD_TYPE_RED) {
            setVisibility( R.id.ispay,View.GONE );
            setVisibility( R.id.isduifang,View.GONE);
            setGlideImagView( R.id.head, data.getHead_pic() );
            setText( R.id.name, data.getReciprocal() );
            setText( R.id.price, data.getMoney() );
            setText( R.id.status, data.getOrder_status_note() );
            setText( R.id.paytype,  getPayType( data.getPay_code() ));
            setText( R.id.time, TimestampTransform.TimeStamp2Date( data.getPay_time())  );
            setText( R.id.number, data.getOrder_sn() );
           // setText( R.id.trade, data.getTrade_type() );
            //setText( R.id.account, data.getReciprocal() );

        }

    }


    public void initSubisidy(ArrayList<RecordPlanData> datas) {
        setVisibility( R.id.subisidy,View.VISIBLE );
        setRecyclerViewLayoutManager( R.id.subisidys, new FullStaggeredGridLayoutManager( 1, StaggeredGridLayoutManager.VERTICAL ) ).setAdapter( new GodBaseAdapter<RecordPlanData>( R.layout.item_record_plan, datas ) {
            @Override
            public void onBind(GodViewHolder holder, int position, RecordPlanData data) {
                holder.setText( R.id.price, "¥" + data.getMoney() );
                holder.setText( R.id.date, data.getExecution_date() );
                if(data.getIsdone().equals( "1" ))
                holder.setText( R.id.status, "已完成" );
                else{
                    holder.setText( R.id.status, "未完成" );
                    holder.setTextColor( R.id.status,getContext().getResources().getColor( R.color.textmiddle ));
                }
            }
        } );

    }

    public void loadData() {

        showProgress();
        Map map = new HttpMap( getContext() );
        map.put( "id", bundleData.getString( "id" ) );
        OkHttpUtils.post().url( HttpUrl.RECORD_DETAILS_TYPE_URL ).params( map ).build().execute( new HttpObjectCallback<RecordOrderData>( getContext() ) {
            @Override
            public void onSuccess(RecordOrderData object, String msg) {
                initData( object );
                dismissProgress();

            }

            @Override
            public void onFail(int code, String msg) {
                dismissProgress();
                ToastUtils.showToast( getContext(), msg );//dismissProgress();
            }


        } );


    }

    public String getPayType(String payid) {
        String payname = "余额支付";
        if(payid==null)return payname;
        if (payid.equals( Constant.PAY_TYPE_BLANANCE ))
            payname = "余额支付";
        if (payid.equals( Constant.PAY_TYPE_CAIBAO ))
            payname = "财币支付";
        if (payid.equals( Constant.PAY_TYPE_WEIXIN ))
            payname = "微信支付";
        if (payid.equals( Constant.PAY_TYPE_ALIPAY ))
            payname = "支付宝支付";
        return payname;
    }

    @Override
    public void OnClickListener(View view) {
        if (R.id.sumbit == view.getId()) {
            skipActivity( UserHelpActivity.class );
        }
    }
}
